package com.bahehe.hsinchubus;

import java.util.ArrayList;
import java.util.HashMap;


import com.kuad.KuBanner;


import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class BusInfo extends ActivityGroup {
	
	    private static final String Tag = "BusInfo";
		ListView mListRouteInfo;
		ArrayList<HashMap<String, Object>> busInfoList = null;
		SimpleAdapter listItemAdapter = null;
		CustomBaseAdapter busInfoAdapter;
		private TextView mInputRouteTextView = null;
		private ArrayList<SearchResults> searchResults = null;
		private Button mButton1 = null;
		private Button mButton2 = null;
		private Button mButton3 = null;
		private Button mButton4 = null;
		private Button mButton5 = null;
		private Button mButton6 = null;
		private Button mButton7 = null;
		private Button mButton8 = null;
		private Button mButton9 = null;
		private Button mButton0 = null;
		private Button mButtonClear = null;
		
		private final static String BUS_NAME="bus_name";
		private final static String BUS_NUMBER="bus_num";
		private final static String BUS_ROUTEID="bus_id";  
		
		private String mSearchRouteNum = "";
	
	   @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);  
	        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
	                WindowManager.LayoutParams.FLAG_FULLSCREEN);  
	        setContentView(R.layout.businfo);
	        
	        
//	        KuBanner banner = (KuBanner)findViewById(R.id.kuadview);
//	        banner.setAPID("000000hUe");
	        
	        findViews();
	        setListensers() ;
	        setupViewComponent() ;
	        
	        
	        try {
	        	searchResults = GetSearchResults();          
	 	        busInfoAdapter = new CustomBaseAdapter(BusInfo.this, searchResults);
	 	        mListRouteInfo.setAdapter(busInfoAdapter);     
			} catch (Exception e) {
				// TODO: handle exception
			}
	          
	        mListRouteInfo.setOnItemClickListener(BusRouteClickListener);
//	   
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
//					long arg3) {
//				  Object o = mListRouteInfo.getItemAtPosition(position);
//		          SearchResults fullObject = (SearchResults)o;		          
//				}  
//	        });
	        
	   }
	   
	   private void setupViewComponent() {
	    	LinearLayout ll = (LinearLayout)findViewById(R.id.adLayout);
	    	ll.setOrientation(LinearLayout.VERTICAL);
	    	
	    	
	        tw.mobiforce.Ad.AdView ad = new tw.mobiforce.Ad.AdView(this);
	        
	        ad.SetIsReloadAd(true);
	        ad.setReloadTime(10);
	        ad.Request(this, 546);
//	        ad.LoadAd(113, 480, 320);
	        
	        ll.addView(ad);
	        

	    }
	   
	   public void findViews(){
		    mInputRouteTextView = (TextView)findViewById(R.id.txtInputRoute);
			mListRouteInfo = (ListView)findViewById(R.id.listRouteResult);
			mButton1 = (Button)findViewById(R.id.buttonNum1);
			mButton2 = (Button)findViewById(R.id.buttonNum2);
			mButton3 = (Button)findViewById(R.id.buttonNum3);
			mButton4 = (Button)findViewById(R.id.buttonNum4);
			mButton5 = (Button)findViewById(R.id.buttonNum5);
			mButton6 = (Button)findViewById(R.id.buttonNum6);
			mButton7 = (Button)findViewById(R.id.buttonNum7);
			mButton8 = (Button)findViewById(R.id.buttonNum8);
			mButton9 = (Button)findViewById(R.id.buttonNum9);
			mButton0 = (Button)findViewById(R.id.buttonNum0);
			mButtonClear = (Button)findViewById(R.id.buttonNumClear);		
		}
		
		
		private void setListensers() {
			mButton1.setOnClickListener(SearchButtonClickListener);
			mButton2.setOnClickListener(SearchButtonClickListener);
			mButton3.setOnClickListener(SearchButtonClickListener);
			mButton4.setOnClickListener(SearchButtonClickListener);
			mButton5.setOnClickListener(SearchButtonClickListener);
			mButton6.setOnClickListener(SearchButtonClickListener);
			mButton7.setOnClickListener(SearchButtonClickListener);
			mButton8.setOnClickListener(SearchButtonClickListener);
			mButton9.setOnClickListener(SearchButtonClickListener);
			mButton0.setOnClickListener(SearchButtonClickListener);
			mButtonClear.setOnClickListener(ResetSearchButtonClickListener);
			
		}
		
//		View oldView;

		private AdapterView.OnItemClickListener BusRouteClickListener = new AdapterView.OnItemClickListener(){

				
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long arg3) {
				// TODO Auto-generated method stub
				
				  if (((ListView)parent).getTag() != null){

					  		((View)((ListView)parent).getTag()).setBackgroundDrawable(null);

					  }

					  ((ListView)parent).setTag(view);
					  view.setBackgroundColor(Color.rgb(102, 204, 255));
					  			//view.setBackgroundResource(R.drawable.bg_title_bustime);

				
				Object o = mListRouteInfo.getItemAtPosition(position);
				SearchResults fullObject = (SearchResults)o;
				Log.i(Tag,"RouteID=" +fullObject.getRoute_id() );
				Log.i(Tag,"Name=" +fullObject.getRoute_name() );
				Log.i(Tag,"Number=" +fullObject.getRoute_number() );
				//fullObject.getRoute_number()
				Bundle bundle = new Bundle();
				bundle.putString(BUS_NAME, fullObject.getRoute_name());
				bundle.putString(BUS_ROUTEID, fullObject.getRoute_id());
				bundle.putString(BUS_NUMBER,fullObject.getRoute_number() );
				
				Intent intent = new Intent(BusInfo.this,BusRouteTime.class);
		    	intent.putExtras(bundle);
		    	startActivity(intent);
			}
			
		};
		
		private Button.OnClickListener ResetSearchButtonClickListener = new Button.OnClickListener(){
		
		     public void onClick(View view){  			 
		    		searchResults = GetSearchResults();
		    		mInputRouteTextView.setText("");	
		    		mSearchRouteNum = "";
		    		busInfoAdapter = new CustomBaseAdapter(BusInfo.this, searchResults);
		    		mListRouteInfo.setAdapter(busInfoAdapter);
		    		busInfoAdapter.notifyDataSetChanged();
		     }
		};
		
		private Button.OnClickListener SearchButtonClickListener = new Button.OnClickListener(){
		
		     public void onClick(View view){		     
		    	 Button b = (Button)view;
		    	 String text = b.getText().toString();
		    	 mSearchRouteNum = mSearchRouteNum +  text.charAt(0);
		    	 if(mSearchRouteNum.length() <=2){			    		 
			    		 mInputRouteTextView.setText(mSearchRouteNum);			    		 
			    		 searchResults = GetSearchResults(mSearchRouteNum);
			    		 busInfoAdapter = new CustomBaseAdapter(BusInfo.this, searchResults);
			    		 Log.i(Tag,"searchResults Count="+searchResults.size());
				    	 mListRouteInfo.setAdapter(busInfoAdapter);
				    	 busInfoAdapter.notifyDataSetChanged();
		    	 }
			  
		     }
		};
		
		
		
		private ArrayList<SearchResults> GetSearchResults(){
		     ArrayList<SearchResults> results = new ArrayList<SearchResults>();
		     
		     String[] arr_route_number = getResources().getStringArray(R.array.RouteNumber);
		     String[] arr_route_name = getResources().getStringArray(R.array.RouteName);
		     String[] arr_route_id = getResources().getStringArray(R.array.RouteId);
		     
		     for(int i=0; i<arr_route_number.length;i+=2){
		    	 SearchResults sr = new SearchResults();
		    	 if(arr_route_number[i].equals("62"))
		    	     sr.setRoute_number(arr_route_number[i]+ "路");
		    	 else
		    		 sr.setRoute_number(arr_route_number[i] + "路(去程)");
			     sr.setRoute_name(arr_route_name[i]);	
			     sr.setRoute_id(arr_route_id[i]);
			     results.add(sr);
			   //  Log.i(Tag,"i=" + i);
			     
			     
			     sr = new SearchResults();
			     if(arr_route_number[i+1].equals("53"))
			    	 sr.setRoute_number(arr_route_number[i+1] + "路(去程)");
			     else	if(arr_route_number[i+1].equals("63"))
			    	 sr.setRoute_number(arr_route_number[i+1] + "路");
			     else
			    	 sr.setRoute_number(arr_route_number[i+1] + "路(回程)");
			     sr.setRoute_name(arr_route_name[i+1]);
			     sr.setRoute_id(arr_route_id[i+1]);
			     results.add(sr);
			     //Log.i(Tag,"i+1=" + (i+1));
		     }	     
		     	 
		     return results;
	    }
		private ArrayList<SearchResults> GetSearchResults(String routeNum){
		     ArrayList<SearchResults> results = new ArrayList<SearchResults>();
		     
		     String[] arr_route_number = getResources().getStringArray(R.array.RouteNumber);
		     String[] arr_route_name = getResources().getStringArray(R.array.RouteName);
		     String[] arr_route_id = getResources().getStringArray(R.array.RouteId);
		     
		     for(int i=0; i<arr_route_number.length;i+=2){
		    	 SearchResults sr = new SearchResults();
		    	 if(routeNum.length()==1){
		    		 if(arr_route_number[i].charAt(0) == routeNum.charAt(0)){
		    			 if(arr_route_number[i].equals("62"))
				    	     sr.setRoute_number(arr_route_number[i]+ "路");
				    	 else
				    		 sr.setRoute_number(arr_route_number[i] + "路(去程)");
						     sr.setRoute_name(arr_route_name[i]);
						     sr.setRoute_id(arr_route_id[i]);
						     results.add(sr);
						     
						     sr = new SearchResults();
						     if(arr_route_number[i+1].equals("53"))
						    	 sr.setRoute_number(arr_route_number[i+1] + "路(去程)");
						     else	if(arr_route_number[i+1].equals("63"))
						    	 sr.setRoute_number(arr_route_number[i+1] + "路");
						     else
						    	 sr.setRoute_number(arr_route_number[i+1] + "路(回程)");
						     sr.setRoute_name(arr_route_name[i+1]);
						     sr.setRoute_id(arr_route_id[i+1]);
						     results.add(sr);
			    	 }
		    	 }else if(routeNum.length()==2 && arr_route_number[i].length()>=2){
		    		 if(arr_route_number[i].charAt(0)==routeNum.charAt(0) &&
		    				 arr_route_number[i].charAt(1)==routeNum.charAt(1)){
		    			 if(arr_route_number[i].equals("62"))
				    	     sr.setRoute_number(arr_route_number[i]+ "路");
				    	 else
				    		 sr.setRoute_number(arr_route_number[i] + "路(去程)");
						     sr.setRoute_name(arr_route_name[i]);
						     sr.setRoute_id(arr_route_id[i]);
						     results.add(sr);
						     
						     sr = new SearchResults();

						     if(arr_route_number[i+1].equals("53"))
						    	 sr.setRoute_number(arr_route_number[i+1] + "路(去程)");
						     else	if(arr_route_number[i+1].equals("63"))
						    	 sr.setRoute_number(arr_route_number[i+1] + "路");
						     else
						    	 sr.setRoute_number(arr_route_number[i+1] + "路(回程)");
						     
						     sr.setRoute_name(arr_route_name[i+1]);
						     sr.setRoute_id(arr_route_id[i+1]);
						     results.add(sr);
			    	 }
		    	 }
		    
			 
		     }	     
		     	 
		     return results;
	    }
}
